package com.glory.dgcpcpda.activity.technicalservice;

import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.entity.EqpInfoEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.LotUtils;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import butterknife.BindView;
import butterknife.OnClick;

public class EngineeringActivity extends BaseActivity{


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.etEqpID)
    FreeUI_ClearEditText etEqpID;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvLocation)
    TextView tvLocation;
    @BindView(R.id.tvDepartment)
    TextView tvDepartment;
    @BindView(R.id.tvEqpType)
    TextView tvEqpType;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_engineering;
    }

    @Override
    protected void initView() {
        tvTitle.setText("工程借机完成");
        setMainLayout(llMain);

    }

    @Override
    protected void initData() {

        initEdit(etEqpID,s ->{
            LotUtils.searchEqpInfo(getText(etEqpID), EngineeringActivity.this, getSupportFragmentManager(), new LotUtils.netCallback<EqpInfoEntity>() {
                @Override
                public void onSuccess(EqpInfoEntity response) {
                    tvLocation.setText(response.getEQUIPMENT().getLOCATION());
                    tvDepartment.setText(response.getEQUIPMENT().getDEPARTMENT());
                    tvEqpType.setText(response.getEQUIPMENT().getEQPTYPE());
                }
                @Override
                public void onFail(Throwable t) {
                    loadError(t,"EQPQUERY");
                }
            });
        });
    }

    @OnClick(R.id.tvBaseSubmit)
    public void onViewClicked() {
        if (getText(etEqpID).equals("")){
            showToast("请扫描设备号",false);
            return;
        }
        FreeUI_EntityCallBack<BaseResponseBean<?>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<?>>
                (new TypeToken<BaseResponseBean<?>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<?>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    showToast("工程借机完成",false);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<?>> response) {
                super.onError(response);
                loadError(response.getException(), "OPPORTUNITYLOGEVENT");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("EQUIPMENTID", getText(etEqpID));
        NetUtils.requestPostNet("CPC.OPPORTUNITYLOGEVENT","TRANSFERMES", this, map, callBack);
    }

}
